package com.bestv.search.engine.command;

import com.bestv.search.common.model.SearchKeywordWrapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Michael Wang
 */
public class HitQueue {

    protected final Log log = LogFactory.getLog(getClass());

    private boolean firstLevelLock = false;
    private List<SearchKeywordWrapper> wrapperFirstLevelList = new ArrayList<SearchKeywordWrapper>();
    private List<SearchKeywordWrapper> wrapperSecondLevelList = new ArrayList<SearchKeywordWrapper>();

    public void push(SearchKeywordWrapper wrapper) {
        if (firstLevelLock) {
            log.debug("First level list locked, push keyword " + wrapper + " into second level list.");
            wrapperSecondLevelList.add(wrapper);
        } else {
            if (wrapperSecondLevelList.size() > 0) {
                wrapperFirstLevelList.addAll(wrapperSecondLevelList);
                wrapperSecondLevelList.clear();
                log.debug("Second level list is clear.");
            }
            wrapperFirstLevelList.add(wrapper);
        }
    }

    public SearchKeywordWrapper pop() {
        if (wrapperFirstLevelList.size() == 0)
            return null;
        else {
            SearchKeywordWrapper wrapper = wrapperFirstLevelList.get(0);
            wrapperFirstLevelList.remove(0);
            return wrapper;
        }
    }

    public List<SearchKeywordWrapper> getHitKeywordList() {
        return wrapperFirstLevelList;
    }

    public boolean isFirstLevelLock() {
        return firstLevelLock;
    }

    public void setFirstLevelLock(boolean firstLevelLock) {
        this.firstLevelLock = firstLevelLock;
    }

    public void clear() {
        this.wrapperFirstLevelList.clear();
    }
}
